{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/16_add_animated_text.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube(\"fDnDVuM_Ke4\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Update the geemap package\n",
    "\n",
    "If you run into errors with this notebook, please uncomment the line below to update the [geemap](https://github.com/gee-community/geemap#installation) package to the latest version from GitHub. \n",
    "Restart the Kernel (Menu -> Kernel -> Restart) to take effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Add animated text to an existing GIF\n",
    "\n",
    "You can download this GIF example from [here](https://github.com/gee-community/geemap/blob/master/examples/data/animation.gif). You can also create GIF images from Earth Engine data using this amazing [LT-GEE Time Series Animator](https://emaprlab.users.earthengine.app/view/lt-gee-time-series-animator), which was created by [Justin Braaten](https://github.com/jdbcode)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_gif = os.path.abspath(\"../data/animation.gif\")\n",
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "out_gif = os.path.join(out_dir, \"output.gif\")\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(in_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "### Add animated text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    in_gif,\n",
    "    out_gif,\n",
    "    xy=(\"5%\", \"5%\"),\n",
    "    text_sequence=1984,\n",
    "    font_size=30,\n",
    "    font_color=\"#0000ff\",\n",
    "    duration=100,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "### Add place name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    out_gif, out_gif, xy=(\"30%\", \"85%\"), text_sequence=\"Las Vegas\", font_color=\"black\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "### Change font type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.system_fonts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    in_gif,\n",
    "    out_gif,\n",
    "    xy=(\"5%\", \"5%\"),\n",
    "    text_sequence=1984,\n",
    "    font_size=30,\n",
    "    font_color=\"#0000ff\",\n",
    "    duration=100,\n",
    ")\n",
    "geemap.add_text_to_gif(\n",
    "    out_gif,\n",
    "    out_gif,\n",
    "    xy=(\"30%\", \"85%\"),\n",
    "    text_sequence=\"Las Vegas\",\n",
    "    font_type=\"timesbd.ttf\",\n",
    "    font_size=30,\n",
    "    font_color=\"black\",\n",
    ")\n",
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "## Create GIF from Earth Engine data\n",
    "\n",
    "This example was adapted from the Earth Engine JavaScript API Documentation [here](https://developers.google.com/earth-engine/ic_visualization#video_thumb)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "### Prepare for an ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "\n",
    "ee.Initialize()\n",
    "\n",
    "# Define an area of interest geometry with a global non-polar extent.\n",
    "aoi = ee.Geometry.Polygon(\n",
    "    [[[-179.0, 78.0], [-179.0, -58.0], [179.0, -58.0], [179.0, 78.0]]], None, False\n",
    ")\n",
    "\n",
    "# Import hourly predicted temperature image collection for northern winter\n",
    "# solstice. Note that predictions extend for 384 hours; limit the collection\n",
    "# to the first 24 hours.\n",
    "tempCol = (\n",
    "    ee.ImageCollection(\"NOAA/GFS0P25\")\n",
    "    .filterDate(\"2018-12-22\", \"2018-12-23\")\n",
    "    .limit(24)\n",
    "    .select(\"temperature_2m_above_ground\")\n",
    ")\n",
    "\n",
    "# Define arguments for animation function parameters.\n",
    "videoArgs = {\n",
    "    \"dimensions\": 768,\n",
    "    \"region\": aoi,\n",
    "    \"framesPerSecond\": 10,\n",
    "    \"crs\": \"EPSG:3857\",\n",
    "    \"min\": -40.0,\n",
    "    \"max\": 35.0,\n",
    "    \"palette\": [\"blue\", \"purple\", \"cyan\", \"green\", \"yellow\", \"red\"],\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21",
   "metadata": {},
   "source": [
    "### Save the GIF to local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "saved_gif = os.path.join(os.path.expanduser(\"~\"), \"Downloads/temperature.gif\")\n",
    "geemap.download_ee_video(tempCol, videoArgs, saved_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(saved_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "### Generate an hourly text sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26",
   "metadata": {},
   "source": [
    "### Add text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = os.path.join(os.path.expanduser(\"~\"), \"Downloads/output2.gif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    saved_gif,\n",
    "    out_gif,\n",
    "    xy=(\"3%\", \"5%\"),\n",
    "    text_sequence=text,\n",
    "    font_size=30,\n",
    "    font_color=\"#ffffff\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    out_gif,\n",
    "    out_gif,\n",
    "    xy=(\"32%\", \"92%\"),\n",
    "    text_sequence=\"NOAA GFS Hourly Temperature\",\n",
    "    font_color=\"white\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
